/**
 * Copyright &copy; 2012-2016 civet All rights reserved.
 */
package cn.mcsys.myadmin.modules.air.service;

import cn.mcsys.myadmin.common.persistence.Page;
import cn.mcsys.myadmin.common.service.CrudService;
import cn.mcsys.myadmin.modules.air.dao.AirRealtimeMessageDao;
import cn.mcsys.myadmin.modules.air.entity.AirRealtimeMessage;
import cn.mcsys.myadmin.modules.air.entity.AirRidePoint;
import cn.mcsys.myadmin.modules.air.vo.AirRealMessageVO;
import cn.mcsys.myadmin.modules.sys.entity.User;
import cn.mcsys.myadmin.modules.sys.utils.UserUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.util.*;

/**
 * 实习消息Service
 *
 * @author leopold
 * @version 2018-08-13
 */
@Service
@Transactional(readOnly = true)
public class AirRealtimeMessageService extends CrudService<AirRealtimeMessageDao, AirRealtimeMessage> {
    @Autowired
    private AirRidePointService airRidePointService;

    public AirRealtimeMessage get(Integer id) {
        return super.get(id);
    }

    public List<AirRealtimeMessage> findList(AirRealtimeMessage airRealtimeMessage) {
        return super.findList(airRealtimeMessage);
    }

    public Page<AirRealtimeMessage> findPage(Page<AirRealtimeMessage> page, AirRealtimeMessage airRealtimeMessage) {
        if (airRealtimeMessage.getAirRidePoint() != null && airRealtimeMessage.getAirRidePoint().getId() == null) {
            airRealtimeMessage.setAirRidePoint(null);
        }
        if(airRealtimeMessage.getFeedbackDate()==null){
            airRealtimeMessage.setFeedbackDate(new Date());
        }
        return super.findPage(page, airRealtimeMessage);
    }

    @Transactional(readOnly = false)
    public void save(AirRealtimeMessage airRealtimeMessage) {
        super.save(airRealtimeMessage);
    }

    @Transactional(readOnly = false)
    public void delete(AirRealtimeMessage airRealtimeMessage) {
        super.delete(airRealtimeMessage);
    }

    /**
     * 设置基本信息
     *
     * @param point
     * @param type
     */
    @Transactional(readOnly = false)
    public void setProperty(AirRidePoint point, int type, String note) {
        User user = UserUtils.getUser();
        AirRealtimeMessage realtimeMessage = new AirRealtimeMessage();
        realtimeMessage.setAirRidePoint(point);
        realtimeMessage.setAirWorker(user.getName());
        realtimeMessage.setFeedbackDate(new Date());
        realtimeMessage.setNote(note);
        realtimeMessage.setType(AirRealtimeMessage.SCHEDULING);
        realtimeMessage.setTitle(realtimeMessage.getTitle());
        save(realtimeMessage);
    }

    /**
     * 查询所有的车辆调度信息
     *
     * @return
     */
    public List<AirRealtimeMessage> selectMessage4type(int type, int limit) {
        return dao.selectMessage4type(type, limit);
    }

    public List<AirRealtimeMessage> getSubmission(String date) {
        return dao.getSubmission(date);
    }

    public void sendMessage(Integer airRidePointId, AirRealtimeMessage airRealtimeMessage, String carNumber) {
        AirRidePoint airRidePoint = airRidePointService.get(airRidePointId);
        airRealtimeMessage.setAirRidePoint(airRidePoint);
        airRealtimeMessage.setFeedbackDate(new Date());
        String note = "";
        switch (airRealtimeMessage.getType()) {
            case 4:
                //粤C123454 已发车,载客50人
                note = carNumber + " 已发车，载客 " + airRealtimeMessage.getNote() + " 人";
                airRealtimeMessage.setNote(note);
                break;
            case 5:
                //粤C123454 现场候车 50 人
                note = carNumber + " 现场候车 " + airRealtimeMessage.getNote() + " 人";
                airRealtimeMessage.setNote(note);
                break;
            default:
                break;
        }
        dao.insert(airRealtimeMessage);
    }

    public Map<String, Integer> getMessageByAirRidePointId(Integer airRidePointId) {
        List<AirRealtimeMessage> list = dao.getMessageByAirRidePointId(airRidePointId);
        Map<String, Integer> map = new HashMap<>();
        Integer waiting = 0, riding = 0, returning = 0, returned = 0;
        for (AirRealtimeMessage message : list) {
            switch (message.getType()) {
                case 4:
                    waiting++;
                    break;
                case 5:
                    riding++;
                    break;
                case 6:
                    returning++;
                    break;
                case 7:
                    returned++;
                    break;
                default:
                    break;
            }
        }
        map.put("waiting", waiting);
        map.put("riding", riding);
        map.put("returning", returning);
        map.put("returned", returned);
        return map;
    }

    public List<AirRealtimeMessage> getMessageByAirRidePointIdWithTime(Integer airRidePointId, String date) {
//        Map<String, Object> map = new HashMap<>();
        List<AirRealtimeMessage> list = dao.getMessageByAirRidePointIdWithTime(airRidePointId, date);
        /*
        Integer seven = 0, sevenH = 0,
                eight = 0, eightH = 0,
                nine = 0, nineH = 0,
                ten = 0, tenH = 0,
                eleven = 0, elevenH = 0,
                twelve = 0, twelveH = 0;
        int[] before = new int[6];
        int[] after = new int[6];
        for (AirRealtimeMessage message : list) {
            Date feedbackDate = message.getFeedbackDate();
            String formatDate = DateUtils.formatDate(feedbackDate, "HH:mm");
            String[] split = formatDate.split(":");
            if (Integer.parseInt(split[1]) < 30) {
                before[Integer.parseInt(split[0]) - 7]++;
            } else {
                after[Integer.parseInt(split[0]) - 7]++;
            }
        }
        return map;
        */
        return list;
    }

    public AirRealMessageVO selectRealMessageTypeCount() {
        AirRealMessageVO airRealMessageVO = new AirRealMessageVO();
        Map<String, Object> map = dao.selectRealMessageTypeCount();
        if (map==null){
            map = new HashMap<>();
        }
        airRealMessageVO.setSchedulingSum(map.get("type1")==null?0:((BigDecimal) map.get("type1")).intValue());
        airRealMessageVO.setAbnormalSum(map.get("type2")==null?0:((BigDecimal) map.get("type2")).intValue());
        airRealMessageVO.setOperationSum(map.get("type3")==null?0:((BigDecimal) map.get("type3")).intValue());
        airRealMessageVO.setSubmissionSum(map.get("type4")==null?0:((BigDecimal) map.get("type4")).intValue());
        return airRealMessageVO;
    }

}